#ifndef usb_gamepad_h__
#define usb_gamepad_h__

#include <stdint.h>
#include <util/delay.h>
//#include "pindefs.h"
#include "dataForController_t.h"
#include "physicalButtonList_t.h"

// Timeout in ms for the USB communication to start during initialization
#define USB_TIMEOUT 1000

uint8_t usb_init(void);			// initialize everything
uint8_t usb_configured(void);		// is the USB port configured

// This defines our gamepad_state_t type.  This is half of changing what data UnoJoy sends out - the other half is changing
//  the HID Report Descriptor in usb_gamepad.c to reflect the different data contained here.

typedef struct {
	// digital buttons, 0 = off, 1 = on

	uint8_t b0_btn : 1;
	uint8_t b1_btn : 1;
	uint8_t b2_btn : 1;
	uint8_t b3_btn : 1;
	uint8_t b4_btn : 1;
	uint8_t b5_btn : 1;
	uint8_t b6_btn : 1;
	uint8_t b7_btn : 1;
	
	uint8_t b8_btn : 1;
	uint8_t b9_btn : 1;
	uint8_t b10_btn : 1;
	uint8_t b11_btn : 1;
	uint8_t b12_btn : 1;
	uint8_t b13_btn : 1;
	uint8_t b14_btn : 1;
	uint8_t b15_btn : 1;
	
	uint8_t b16_btn : 1;
	uint8_t b17_btn : 1;
	uint8_t b18_btn : 1;
	uint8_t b19_btn : 1;
	uint8_t b20_btn : 1;
	uint8_t b21_btn : 1;
	uint8_t b22_btn : 1;
	uint8_t b23_btn : 1;
	
	uint8_t b24_btn : 1;
	uint8_t b25_btn : 1;
	uint8_t b26_btn : 1;
	uint8_t b27_btn : 1;
	uint8_t b28_btn : 1;
	uint8_t b29_btn : 1;
	uint8_t b30_btn : 1;
	uint8_t b31_btn : 1;
	
	uint8_t b32_btn : 1;
	uint8_t b33_btn : 1;
	uint8_t b34_btn : 1;
	uint8_t b35_btn : 1;
	uint8_t b36_btn : 1;
	uint8_t b37_btn : 1;
	uint8_t b38_btn : 1;
	uint8_t b39_btn : 1;
	
	uint8_t b40_btn : 1;
	uint8_t b41_btn : 1;
	uint8_t b42_btn : 1;
	uint8_t b43_btn : 1;
	uint8_t b44_btn : 1;
	uint8_t b45_btn : 1;
	uint8_t b46_btn : 1;
	uint8_t b47_btn : 1;
	
	uint8_t b48_btn : 1;
	uint8_t b49_btn : 1;
	uint8_t b50_btn : 1;
	uint8_t b51_btn : 1;
	uint8_t b52_btn : 1;
	uint8_t b53_btn : 1;
	uint8_t b54_btn : 1;
	uint8_t b55_btn : 1;
	
	uint8_t b56_btn : 1;
	uint8_t b57_btn : 1;
	uint8_t b58_btn : 1;
	uint8_t b59_btn : 1;
	uint8_t b60_btn : 1;
	uint8_t b61_btn : 1;
	uint8_t b62_btn : 1;
	uint8_t b63_btn : 1;
	
	uint8_t b64_btn : 1;
	uint8_t b65_btn : 1;
	uint8_t b66_btn : 1;
	uint8_t b67_btn : 1;
	uint8_t b68_btn : 1;
	uint8_t b69_btn : 1;
	uint8_t b70_btn : 1;
	uint8_t b71_btn : 1;
	
	uint8_t b72_btn : 1;
	uint8_t b73_btn : 1;
	uint8_t b74_btn : 1;
	uint8_t b75_btn : 1;
	uint8_t b76_btn : 1;
	uint8_t b77_btn : 1;
	uint8_t b78_btn : 1;
	uint8_t b79_btn : 1;
	
	uint8_t b80_btn : 1;
	uint8_t b81_btn : 1;
	uint8_t b82_btn : 1;
	uint8_t b83_btn : 1;
	uint8_t b84_btn : 1;
	uint8_t b85_btn : 1;
	uint8_t b86_btn : 1;
	uint8_t b87_btn : 1;
	
	uint8_t b88_btn : 1;
	uint8_t b89_btn : 1;
	uint8_t b90_btn : 1;
	uint8_t b91_btn : 1;
	uint8_t b92_btn : 1;
	uint8_t b93_btn : 1;
	uint8_t b94_btn : 1;
	uint8_t b95_btn : 1;
	
	uint8_t b96_btn : 1;
	uint8_t b97_btn : 1;
	uint8_t b98_btn : 1;
	uint8_t b99_btn : 1;
	uint8_t b100_btn : 1;
	uint8_t b101_btn : 1;
	uint8_t b102_btn : 1;
	uint8_t b103_btn : 1;
	
	uint8_t b104_btn : 1;
	uint8_t b105_btn : 1;
	uint8_t b106_btn : 1;
	uint8_t b107_btn : 1;
	uint8_t b108_btn : 1;
	uint8_t b109_btn : 1;
	uint8_t b110_btn : 1;
	uint8_t b111_btn : 1;
	
	uint8_t b112_btn : 1;
	uint8_t b113_btn : 1;
	uint8_t b114_btn : 1;
	uint8_t b115_btn : 1;
	uint8_t b116_btn : 1;
	uint8_t b117_btn : 1;
	uint8_t b118_btn : 1;
	uint8_t b119_btn : 1;
	
	uint8_t b120_btn : 1;
	uint8_t b121_btn : 1;
	uint8_t b122_btn : 1;
	uint8_t b123_btn : 1;
	uint8_t b124_btn : 1;
	uint8_t b125_btn : 1;
	uint8_t b126_btn : 1;
	uint8_t b127_btn : 1;


	// left and right analog sticks, 0x00 left/up, 0x80 middle, 0xff right/down


	uint8_t a0_axis;
	uint8_t a1_axis;
	uint8_t a2_axis;
	uint8_t a3_axis;
	uint8_t a4_axis;
	uint8_t a5_axis;
	uint8_t a6_axis;
	uint8_t a7_axis;
	uint8_t a8_axis;
	uint8_t a9_axis;
	uint8_t a10_axis;
	uint8_t a11_axis;
	uint8_t a12_axis;
	uint8_t a13_axis;
	uint8_t a14_axis;
	uint8_t a15_axis;
	uint8_t a16_axis;
	uint8_t a17_axis;
	uint8_t a18_axis;
	uint8_t a19_axis;
	uint8_t a20_axis;
	uint8_t a21_axis;
	uint8_t a22_axis;
	uint8_t a23_axis;
	uint8_t a24_axis;
	uint8_t a25_axis;
	uint8_t a26_axis;
	uint8_t a27_axis;
	uint8_t a28_axis;
	uint8_t a29_axis;
	uint8_t a30_axis;
	uint8_t a31_axis;
	uint8_t a32_axis;
	uint8_t a33_axis;
	uint8_t a34_axis;
	uint8_t a35_axis;
	uint8_t a36_axis;
	uint8_t a37_axis;
	uint8_t a38_axis;
	uint8_t a39_axis;
	uint8_t a40_axis;
	uint8_t a41_axis;
	uint8_t a42_axis;
	uint8_t a43_axis;
	uint8_t a44_axis;
	uint8_t a45_axis;
	uint8_t a46_axis;
	uint8_t a47_axis;
	uint8_t a48_axis;			
	
	


	// button axis, 0x00 = unpressed, 0x01 = fully pressed

	uint8_t b0_axis;
	uint8_t b1_axis;
	uint8_t b2_axis;
	uint8_t b3_axis;
	uint8_t b4_axis;
	uint8_t b5_axis;
	uint8_t b6_axis;
	uint8_t b7_axis;
	uint8_t b8_axis;
	uint8_t b9_axis;
	uint8_t b10_axis;
	uint8_t b11_axis;
	uint8_t b12_axis;
	uint8_t b13_axis;
	uint8_t b14_axis;
	uint8_t b15_axis;
	uint8_t b16_axis;
	uint8_t b17_axis;
	uint8_t b18_axis;
	uint8_t b19_axis;
	uint8_t b20_axis;
	uint8_t b21_axis;
	uint8_t b22_axis;
	uint8_t b23_axis;
	uint8_t b24_axis;
	uint8_t b25_axis;
	uint8_t b26_axis;
	uint8_t b27_axis;
	uint8_t b28_axis;
	uint8_t b29_axis;
	uint8_t b30_axis;
	uint8_t b31_axis;
	uint8_t b32_axis;
	uint8_t b33_axis;
	uint8_t b34_axis;
	uint8_t b35_axis;
	uint8_t b36_axis;
	uint8_t b37_axis;
	uint8_t b38_axis;
	uint8_t b39_axis;
	uint8_t b40_axis;
	uint8_t b41_axis;
	uint8_t b42_axis;
	uint8_t b43_axis;
	uint8_t b44_axis;
	uint8_t b45_axis;
	uint8_t b46_axis;
	uint8_t b47_axis;
	uint8_t b48_axis;
	uint8_t b49_axis;
	uint8_t b50_axis;
	uint8_t b51_axis;
	uint8_t b52_axis;
	uint8_t b53_axis;
	uint8_t b54_axis;
	uint8_t b55_axis;
	uint8_t b56_axis;
	uint8_t b57_axis;
	uint8_t b58_axis;
	uint8_t b59_axis;
	uint8_t b60_axis;
	uint8_t b61_axis;
	uint8_t b62_axis;
	uint8_t b63_axis;
	uint8_t b64_axis;
	uint8_t b65_axis;
	uint8_t b66_axis;
	uint8_t b67_axis;
	uint8_t b68_axis;
	uint8_t b69_axis;
	uint8_t b70_axis;
	uint8_t b71_axis;
	uint8_t b72_axis;
	uint8_t b73_axis;
	uint8_t b74_axis;
	uint8_t b75_axis;
	uint8_t b76_axis;
	uint8_t b77_axis;
	uint8_t b78_axis;
	uint8_t b79_axis;
	uint8_t b80_axis;
	uint8_t b81_axis;
	uint8_t b82_axis;
	uint8_t b83_axis;
	uint8_t b84_axis;
	uint8_t b85_axis;
	uint8_t b86_axis;
	uint8_t b87_axis;
	uint8_t b88_axis;
	uint8_t b89_axis;
	uint8_t b90_axis;
	uint8_t b91_axis;
	uint8_t b92_axis;
	uint8_t b93_axis;
	uint8_t b94_axis;
	uint8_t b95_axis;
	uint8_t b96_axis;
	uint8_t b97_axis;
	uint8_t b98_axis;
	uint8_t b99_axis;
	uint8_t b100_axis;
	uint8_t b101_axis;
	uint8_t b102_axis;
	uint8_t b103_axis;
	uint8_t b104_axis;
	uint8_t b105_axis;
	uint8_t b106_axis;
	uint8_t b107_axis;
	uint8_t b108_axis;
	uint8_t b109_axis;											
	uint8_t b110_axis;
	uint8_t b111_axis;
	uint8_t b112_axis;
	uint8_t b113_axis;
	uint8_t b114_axis;
	uint8_t b115_axis;
	uint8_t b116_axis;
	uint8_t b117_axis;
	uint8_t b118_axis;
	uint8_t b119_axis;
	uint8_t b120_axis;
	uint8_t b121_axis;
	uint8_t b122_axis;
	uint8_t b123_axis;
	uint8_t b124_axis;
	uint8_t b125_axis;
	uint8_t b126_axis;
	uint8_t b127_axis;

} gamepad_state_t;

extern gamepad_state_t gamepad_state;

void usb_gamepad_reset_state(void);

int8_t usb_gamepad_send(void);

int8_t sendPS3Data(dataForController_t);


// Everything below this point is only intended for usb_gamepad.c
#ifdef USB_GAMEPAD_PRIVATE_INCLUDE
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

#define EP_TYPE_CONTROL			0x00
#define EP_TYPE_BULK_IN			0x81
#define EP_TYPE_BULK_OUT		0x80
#define EP_TYPE_INTERRUPT_IN		0xC1
#define EP_TYPE_INTERRUPT_OUT		0xC0
#define EP_TYPE_ISOCHRONOUS_IN		0x41
#define EP_TYPE_ISOCHRONOUS_OUT		0x40

#define EP_SINGLE_BUFFER		0x02
#define EP_DOUBLE_BUFFER		0x06

#define EP_SIZE(s)	((s) == 64 ? 0x30 :	\
			((s) == 32 ? 0x20 :	\
			((s) == 16 ? 0x10 :	\
			             0x00)))

#define MAX_ENDPOINT		4

#define LSB(n) (n & 255)
#define MSB(n) ((n >> 8) & 255)

#if defined(__AVR_AT90USB162__)
#define HW_CONFIG()
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
#define USB_CONFIG() (USBCON = (1<<USBE))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_ATmega8U2__)
#define HW_CONFIG()
#define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<2)))
#define USB_CONFIG() (USBCON = (1<<USBE))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_ATmega32U4__)
#define HW_CONFIG() (UHWCON = 0x01)
#define PLL_CONFIG() (PLLCSR = 0x12)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_AT90USB646__)
#define HW_CONFIG() (UHWCON = 0x81)
#define PLL_CONFIG() (PLLCSR = 0x1A)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
#define HW_CONFIG() (UHWCON = 0x81)
#define PLL_CONFIG() (PLLCSR = 0x16)
#define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
#define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
#endif

// standard control endpoint request types
#define GET_STATUS			0
#define CLEAR_FEATURE			1
#define SET_FEATURE			3
#define SET_ADDRESS			5
#define GET_DESCRIPTOR			6
#define GET_CONFIGURATION		8
#define SET_CONFIGURATION		9
#define GET_INTERFACE			10
#define SET_INTERFACE			11
// HID (human interface device)
#define HID_GET_REPORT			1
#define HID_GET_IDLE			2
#define HID_GET_PROTOCOL		3
#define HID_SET_REPORT			9
#define HID_SET_IDLE			10
#define HID_SET_PROTOCOL		11
// CDC (communication class device)
#define CDC_SET_LINE_CODING		0x20
#define CDC_GET_LINE_CODING		0x21
#define CDC_SET_CONTROL_LINE_STATE	0x22
#endif
#endif
